<?php

namespace App\Services;

use App\Models\Area;
use App\Models\Good;
use App\Models\GoodOrder;
use App\Models\Order;
use App\Models\OrderCommission;
use App\Models\OrderDetail;
use App\Models\PaymentOrder;
use App\Models\ServiceOrder;
use App\Models\ShopUser;
use App\Models\User;
use App\Models\UserMoney;
use Exception;

class OrderService
{

    /**
     * 添加充值订单
     * @param $user_id
     * @param $out_trade_no
     * @param $amount
     * @param array $extends 扩展参数
     * @return bool
     */
    public static function addMoneyOrder($user_id,$out_trade_no,$amount, array $extends = []): bool
    {
        $paymentOrder = new PaymentOrder;
        $paymentOrder->type = 0; //充值订单
        $paymentOrder->user_id = $user_id;
        $paymentOrder->shop_id = 0;
        $paymentOrder->out_trade_no = $out_trade_no;
        $paymentOrder->amount = $amount;
        $paymentOrder->extends = $extends;
        return $paymentOrder->save();
    }

    /**
     * 添加洗车订单
     * @param $user_id
     * @param $out_trade_no
     * @param $amount
     * @param int $pay_way
     * @param array $extends 扩展参数
     * @return bool
     */
    public static function addServiceOrder($user_id, $out_trade_no, $amount, int $pay_way = 0, array $extends = []): bool
    {
        $paymentOrder = new PaymentOrder;
        $paymentOrder->type = 2;//洗车订单
        $paymentOrder->pay_way = $pay_way;
        $paymentOrder->user_id = $user_id;
        $paymentOrder->shop_id = $extends['shop_id'];
        $paymentOrder->out_trade_no = $out_trade_no;
        $paymentOrder->amount = $amount;
        $paymentOrder->extends = $extends;
        return $paymentOrder->save();
    }


    /**
     * 添加商品订单
     * @param $params
     * @return bool
     */
    public static function addGoodOrder($params): bool
    {
        $pay_way = $params['pay_way'];
        $user_id = $params['user_id'];
        $out_trade_no = $params['out_trade_no'];
        $amount = $params['amount'];
        $paymentOrder = new PaymentOrder;
        $paymentOrder->type = 1;
        $paymentOrder->pay_way = $pay_way?1:0;
        $paymentOrder->user_id = $user_id;
        $paymentOrder->out_trade_no = $out_trade_no;
        $paymentOrder->amount = $amount;
        $time = now();
        $day = now()->format("Y-m-d H:i:s");
        if ($pay_way==0){
            $paymentOrder->pay_status = 1;
            $paymentOrder->pay_at = $time;
        }
        $res = $paymentOrder->save();
        if ($res){
            $order = new GoodOrder();
            $order->order_no = $out_trade_no;
            $order->user_id = $user_id;
            $order->amount = $amount;
            $order->courier_fee = $params['courier_fee'];
            $order->pay_way = $pay_way;
            $order->method = $params['method'];
            $order->address = $params['address'];
            $order->province_id = $params['province_id'];
            $order->city_id = $params['city_id'];
            $order->district_id = $params['district_id'];
            if ($pay_way==0){
                $order->state = 1;
                $order->pay_time = $day;
            }else{
                $order->state = 0;
            }
            $order->order_time = $day;
            foreach ($params['cars'] as $key=>$car){
                $params['cars'][$key]['oid'] = $out_trade_no;
            }
            $order->details()->createMany($params['cars']);
            $res = $order->save();
            if ($res&&$pay_way==0){
                UserMoney::money(2,$user_id, -floatval($amount), "购买商品");
            }
        }
        return $res;
    }

    /**
     * 添加洗车卡订单
     * @param $user_id
     * @param $out_trade_no
     * @param $amount
     * @param array $extends 扩展参数
     * @return bool
     */
    public static function addCardOrder($user_id,$out_trade_no,$amount,array $extends = []): bool
    {
        $paymentOrder = new PaymentOrder;
        $paymentOrder->type = 3;//洗车卡订单
        $paymentOrder->pay_way = 1;
        $paymentOrder->user_id = $user_id;
        $paymentOrder->shop_id = $extends['shop_id'];
        $paymentOrder->out_trade_no = $out_trade_no;
        $paymentOrder->amount = $amount;
        $paymentOrder->extends = $extends;
        return $paymentOrder->save();
    }

    /**
     * 处理商品确认收货
     */
    public static function confirmGoodOrder($order_no): bool
    {
        $order = GoodOrder::whereOrderNo($order_no)->first();
        if ($order->state==1){
            throw new Exception("暂未发货");
        }
        $order->finish_time = now()->format("Y-m-d H:i:s");
        $order->state = 3;
        return $order->save();
    }

    /**
     * @param $user_id
     * @param $order_no
     * @return bool
     * @throws Exception
     */
    public static function scanGoodOrder($user_id,$order_no): bool
    {
        $user =  User::with('worker')->withExists('worker')->where("id",$user_id)->first();
        if (!$user->worker_exists||!$user->worker){
            throw new Exception("暂无权限");
        }
        $worker = $user->worker;
        $order = GoodOrder::whereOrderNo($order_no)->first();
        if (!$order->method){
            throw new Exception("暂无权限");
        }
        $shop_id = $order->shop_id;
        if ($worker->shop_id!=$shop_id){
            throw new Exception("暂无权限");
        }
        if ($order->state==1){
            throw new Exception("暂未发货");
        }
        if ($order->state==3){
            throw new Exception("已完成");
        }
        $order->finish_time = now()->format("Y-m-d H:i:s");
        $order->state = 3;
        return $order->save();
    }


    /**
     * 查询地址
     * @param $address
     * @return array
     */
    public static function findAddressIds($address): array
    {
        $province_id = 0;
        $city_id = 0;
        $district_id = 0;
        if ($address){
            unset($address['address']);
            unset($address['result']);
            $mergename = implode(",",$address);
            $area = Area::query()->where("name",$address['district'])->where("mergename","like","%{$mergename}%")->first();
            if ($area){
                $district_id = $area->id;
                $city_id = $area->pid;
                $city = Area::whereId($city_id)->first();
                $province_id = $city->pid;
            }
        }
        return [$province_id,$city_id,$district_id];
    }


}
